<template>
  <div class="index">
    <header>
      <h1>呤阅读教务系统</h1>
      <el-popover placement="bottom-start" trigger="hover">
        <span class="logout" @click="logout">退出登录</span>

        <template #reference>
          <span
            ><el-icon><user-filled /></el-icon> {{ userName }}</span
          >
        </template>
      </el-popover>
      <!-- <el-tooltip v-model:visible="visible" class="logout-tip">
        <template #content>
          <span class="logout" @click="logout">退出登录</span>
        </template>
        <span
          ><el-icon><user-filled /></el-icon> {{ userName }}</span
        >
      </el-tooltip> -->
    </header>
    <div class="cont">
      <div class="left">
        <el-row class="tac">
          <el-col>
            <el-menu
              :default-active="active.toString()"
              class="el-menu-vertical-demo"
            >
              <el-menu-item
                :index="index.toString()"
                v-for="(item, index) in menu"
                :key="item.id"
                @click="toDetail(item.path, index)"
              >
                <span>{{ item.title }}</span>
              </el-menu-item>
            </el-menu>
          </el-col>
        </el-row>
      </div>
      <div class="right">
        <router-view />
      </div>
    </div>
  </div>
</template>
<script>
import { toRefs, reactive, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";

export default {
  name: "Index",
  setup() {
    const router = useRouter(); //路由跳转
    const proxy = getCurrentInstance().appContext.config.globalProperties;
    /**
     * 菜单栏跳转路由
     */
    let toDetail = (path, index) => {
      window.localStorage.setItem("activeIndex", index);
      window.localStorage.setItem("path", path);
      router.push({ path: path });
    };
    /**
     * 菜单栏
     */
    /**
     * 定义响应式变量
     */
    let dataObj = reactive({
      menu: [],
      active: window.localStorage.getItem("activeIndex")
        ? window.localStorage.getItem("activeIndex")
        : 0,
      path: window.localStorage.getItem("path"),
      visible: false, //是否显示退出登录气泡
      userName: "", //登录用户名
    });
    let getUserInfo = () => {
      proxy.$https.get("/api/v1/me").then((res) => {
        dataObj.userName = res.data.name;
        var arr = res.data.Roles.filter((item) => item.RoleName == "校区主管");

        if (arr.length > 0) {
          dataObj.menu = [
            {
              title: "转介绍leads",
              path: "/leads",
              id: 1,
              hidden: true,
            },
            {
              title: "学情",
              path: "/learningCondition",
              id: 2,
            },
            {
              title: "积分详情",
              path: "/integralDetails",
              id: 3,
            },
            {
              title: "管理者视图",
              path: "/manager",
              id: 4,
            },
          ];
        } else {
          dataObj.menu = [
            {
              title: "转介绍leads",
              path: "/leads",
              id: 1,
              hidden: true,
            },
            {
              title: "学情",
              path: "/learningCondition",
              id: 2,
            },
            {
              title: "积分详情",
              path: "/integralDetails",
              id: 3,
            },
          ];
        }

        window.localStorage.setItem("userName", res.data.name);
      });
    };

    getUserInfo();
    console.log(dataObj.menu);

    /**
     * 判断是否有缓存路由，如果有的话跳转
     */
    if (dataObj.path) {
      router.push({ path: dataObj.path });
    }
    /**
     * 退出登录
     */
    let logout = () => {
      router.push({ path: "/" });
      window.localStorage.removeItem("access_token");
      window.localStorage.removeItem("activeIndex");
      window.localStorage.removeItem("path");
    };
    return {
      ...toRefs(dataObj),
      toDetail: toDetail,
      logout: logout,
    };
  },
};
</script>
<style scoped lang="scss">
.index {
  width: 100%;
  height: 100%;
  header {
    width: 100%;
    height: 80px;
    background: #b2bee2;
    padding: 0 40px;
    box-sizing: border-box;
    line-height: 80px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    h1 {
      font-size: 20px;
      color: #fff;
      letter-spacing: 0.7px;
    }
    span {
      color: #fff;
      height: 25px;
      display: flex;
      align-items: center;
      cursor: pointer;
      i {
        font-size: 18px;
        margin-right: 5px;
      }
    }
  }
  .cont {
    width: 100%;
    height: calc(100% - 80px);
    display: flex;
    .left {
      width: 200px;
      .el-row {
        width: 100%;
        height: 100%;
        ul {
          width: 100%;
          height: 100%;
          background: #f5f5f5;
          border-right: none;
          li {
            span {
              font-size: 16px;
              color: #666;
            }
          }
          .is-active {
            background: #ecf5ff;
            span {
              color: #7b96e9;
            }
          }
        }
      }
    }
    .right {
      width: calc(100% - 200px);
    }
  }
}
</style>
<style lang="scss">
.logout {
  cursor: pointer !important;
  display: block;
  text-align: center;
}
</style>